<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Options_Model extends MY_Model {
    public function __construct()
    {
        parent::__construct();
    }

    public function get($key)
    {
        $this->db->select('id, opt_key, opt_val');
        $this->db->from($this->table_options_name);
        if (is_array($key)) {
            foreach ($key as $k) {
                $this->db->or_where('opt_key', $k);
            }
        } else {
            $this->db->where('opt_key', $key);
        }

        if (is_array($key)) {
            $aResult = new stdClass();
            $aReturn = $this->db->get()->result();    
            foreach ($aReturn as $r) {
                $name = $r->opt_key;
                $aResult->$name = $r;
            }            
            return $aResult;
        }

        $row = $this->db->get()->first_row();
        if ($row != null) {
            return $row->opt_val;
        }

        return null;
    }

    public function update($data)
    {
        $this->db->trans_begin();
        foreach($data as $key=>$value){
            $this->db->where('id', $value['id']);
            $this->db->update($this->table_options_name, array('opt_val'=>$value['value']));
            if ($this->db->trans_status() === false) {
                $this->db->trans_rollback();
                return false;
            }
        }
        $this->db->trans_commit();
        return true;
    }
}
?>